#include "Parser.h"

void Parser::parsear() {

	int length= 300;
	char buffer[length];
	std::string str_buffer;
	bool found_refs= false;
	bool found_miss= false;
	bool found_rate= false;
	double refs, miss, rate;
	while(!found_refs || !found_miss || !found_rate) {
		std::cin.getline(buffer, length);
		str_buffer.assign(buffer);
		if(!found_refs && ((refs= parser(str_buffer, REFS)) != -1))
			found_refs= true;
		if(!found_miss && ((miss= parser(str_buffer, MISS)) != -1))
			found_miss= true;
		if(!found_rate && ((rate= parser(str_buffer, RATE)) != -1))
			found_rate= true;
	}

	std::cout << "Refs: " << refs << std::endl;
	std::cout << "Miss: " << miss << std::endl;
	std::cout << "Rate: " << rate << std::endl;
}

double Parser::parser(std::string input, std::string to_find) {
	size_t start_pos= input.find(to_find);
	if(start_pos == std::string::npos)
		return -1;
	size_t end_pos= input.find_first_of(END, start_pos);
	size_t lenght = to_find.length();
	std::string line= input.substr(start_pos+lenght, end_pos-start_pos-lenght);
	std::string snum= "";
	for (std::string::iterator it = line.begin(); it < line.end(); ++it) {
		if(*it != ' ') {
			if(*it == ',') *it= '.';
			snum+= *it;
		}
	}
	return atof(snum.c_str());
}

int Parser::getRefsD1() {
  return refsD1;
}

int Parser::getmissD1() {
  return missD1;
}

int Parser::getmissRateD1() {
  return missRateD1;
}

